package com.mlnx.device.session.manager;

import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.device.service.DeviceStateService;
import com.mlnx.device.session.core.DeviceSession;
import com.mlnx.device.session.core.EcgDeviceSession;
import com.mlnx.device.session.core.MpDeviceSession;
import com.mlnx.mptp.model.DeviceState;
import com.mlnx.tp.base.model.DeviceType;
import io.netty.channel.Channel;
import org.shan.mq.session.core.MemmerySessionManager;
import org.shan.mq.session.core.Session;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by amanda.shan on 2019/12/2.
 */
public class MutilDeviceSessionManager extends MemmerySessionManager {

    protected List<Session> ecgSessions = new ArrayList<>();

    protected List<Session> configSessions = new ArrayList<>();

    @Autowired
    private DeviceStateService deviceStateService;

    @Override
    public synchronized void addSession(Session session) {
        super.addSession(session);

        if (session instanceof EcgDeviceSession) {
            ecgSessions.add(session);
        }
    }

    @Override
    public synchronized void removeSession(Session session) {
        super.removeSession(session);
        ecgSessions.remove(session);
        if (session instanceof EcgDeviceSession) {
            DeviceStateRecord deviceStateRecord = new DeviceStateRecord();
            deviceStateRecord.setDeviceId(((EcgDeviceSession) session).getDeviceId());
            deviceStateRecord.setDeviceState(DeviceState.DEVICE_OFFLINE);
            deviceStateRecord.setPatientId(((EcgDeviceSession) session).getPatientId());
            deviceStateRecord.setDate(new Date());
            deviceStateService.saveDeviceState(deviceStateRecord);
        }
    }

    @Override
    public void removeSession(String clientId) {
        Session session = getSession(clientId);
        if (session != null) {
            removeSession(session);
        } else {
            log.info("{} 设备没有在线的session", clientId);
        }
    }

    public Channel getChannel(String deviceId) {
        DeviceSession session = (DeviceSession) getSession(deviceId);
        if (session == null) {
            return null;
        } else {
            return session.getChannel();
        }
    }

    /**
     * 根据病人ID和设备类型获取session
     *
     * @param patientId
     * @param deviceType
     * @return
     */
    public Session getSession(Integer patientId, DeviceType deviceType) {

        for (Session session : getSessionClientIdMap().values()) {
            DeviceSession deviceSession = (DeviceSession) session;
            switch (deviceType) {
                case ECG_DEVICE:
                    if (deviceSession instanceof EcgDeviceSession && deviceSession.getPatientId().equals(patientId)) {
                        return session;
                    }
                    break;
                case MP_DEVICE:
                    if (deviceSession instanceof MpDeviceSession && deviceSession.getPatientId().equals(patientId)) {
                        return session;
                    }
                    break;
                default:

            }
        }
        return null;
    }

    public Collection<String> getEcgDeviceIds() {
        List<String> ecgDeviceIds =
                ecgSessions.stream().filter(session -> session instanceof EcgDeviceSession)
                        .map(session -> session.getClientId()).collect(Collectors.toList());
        return ecgDeviceIds;
    }

    public Collection<String> getOnlineDeviceIds(Collection<String> askDeviceIds) {
        List<String> list =
                getSessionChannelMap().values().stream()
                        .filter(session -> askDeviceIds.contains(session.getClientId()))
                        .map(session -> session.getClientId()).collect(Collectors.toList());
        return list;
    }

    public void refreshLastTime(Channel channel) {
        Session session = getSessionChannelMap().get(channel);
        if (session != null) {
            session.setLastPacketTime(new Date());
        }
    }

    @Override
    public String getName() {
        return "MutilDeviceSessionManager";
    }
}
